/**
 * 初始化通知详情对话框
 */
var LeftMenuInfoDlg = {
    leftMenuInfoData: {},
    ztreeInstance: null,
    editor: null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '名称不能为空'
                }
            }
        },
        code: {
            validators: {
                notEmpty: {
                    message: '编码不能为空'
                }
            }
        },
        jumpType: {
            validators: {
                notEmpty: {
                    message: '跳转类型不能为空'
                }
            }
        },
        pri: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
LeftMenuInfoDlg.clearData = function () {
    this.leftMenuInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
LeftMenuInfoDlg.set = function (key, value) {
    this.leftMenuInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
LeftMenuInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
LeftMenuInfoDlg.close = function () {
    parent.layer.close(window.parent.LeftMenu.layerIndex);
}

/**
 * 收集数据
 */
LeftMenuInfoDlg.collectData = function () {
    this.leftMenuInfoData['content'] = LeftMenuInfoDlg.editor.txt.html();
    this.set('id').set('name').set('pri').set('code').set('pri').set('jumpType').set('linkUrl') ;
}

/**
 * 验证数据是否为空
 */
LeftMenuInfoDlg.validate = function () {
    $('#leftMenuInfoForm').data("bootstrapValidator").resetForm();
    $('#leftMenuInfoForm').bootstrapValidator('validate');
    return $("#leftMenuInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
LeftMenuInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    //右边栏目父ID设置为0
    this.leftMenuInfoData['parentid'] = 0;

    if (!this.validate()) {
        return;
    }
    if (isNaN(this.leftMenuInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/menu/add/left", function (data) {
        Feng.success("添加成功!");
        window.parent.LeftMenu.table.refresh();
        LeftMenuInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.leftMenuInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
LeftMenuInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    //右边栏目父ID设置为0
    this.leftMenuInfoData['parentid'] = 0;

    if (!this.validate()) {
        return;
    }
    if (isNaN(this.leftMenuInfoData.pri)) {
        Feng.error('排序值请输入数字！');
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/hp/menu/update", function (data) {
        Feng.success("修改成功!");
        window.parent.LeftMenu.table.refresh();
        LeftMenuInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.leftMenuInfoData);
    ajax.start();
}


/**
 * 选择框值变化时
 */
LeftMenuInfoDlg.onSelectChange = function () {
    var sValue = $("#jumpType").val();
    if(sValue == 'details'){
        $("#editorForm").show();
    }else if(sValue == 'othersite'){
        $("#linkUrl").attr('readonly',false);
    }else{
        $("#editorForm").hide();
        $("#linkUrl").attr('readonly',true);
    }
};

$(function () {
    Feng.initValidator("leftMenuInfoForm", LeftMenuInfoDlg.validateFields);

    //初始化是否展示选项
    $("#jumpType").val($("#jumpTypeVal").val());
    $("#jumpType").change(LeftMenuInfoDlg.onSelectChange);

    if($("#jumpType").val() == 'othersite'){
        $("#linkUrl").attr('readonly',false);
    }else if($("#jumpType").val() == 'details'){
        $("#editorForm").show();
    }

    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    //editor.customConfig.uploadImgShowBase64 = true;
    editor.customConfig.uploadImgServer = '/hp/pic/wangeditorUpload';
    editor.customConfig.uploadFileName = 'picture';
    editor.create();
    editor.txt.html($("#contentVal").val());
    LeftMenuInfoDlg.editor = editor;
});
